<?php
/**
 * Created and Edit Amway account infomation
 * @category  amway
 * @package   StatusNet
 * @author    cuongnh (hungcuongbs@gmail.com)
 * @author    thuclh <thuc.lehuy@gmail.com>
 * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
 * @link      http://status.net/
 */

if (!defined('STATUSNET') && !defined('LACONICA')) {
    exit(1);
}
require_once INSTALLDIR.'/lib/amwayservicesaction.php';

class AmwayaccountAction extends AmwayServiceAction {

    var $msg;

    function title() {
        return sprintf(_('Amway account infomation'));
    }

    /**
     * Prepare to run
     */

    function prepare($args) {
        parent::prepare($args);

        if (!common_logged_in()) {
            $this->clientError(_('You must be logged in to edit amway account.'));
            return false;
        }
        // get amway info
        $cur = common_current_user();
        $this->amway = $cur->getAmwayInfo();
        return true;
    }

    /**
     * Handle the request
     *
     * On GET, show the form. On POST, try to save the group.
     *
     * @param array $args unused
     *
     * @return void
     */

    function handle($args) {
        parent::handle($args);
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            if($this->amway) {
                $this->trySave();
            }else {
                $this->tryInsert();
            }
        } else {
            $this->showForm();
        }
    }

    function showForm($msg=null) {
        $this->msg = $msg;
        $this->showPage();
    }

    function showContent() {
        $form = new AmwayaccountForm($this, $this->amway);
        $form->show();
    }

    function showPageNotice() {
        if ($this->msg) {
            $this->element('p', 'error', $this->msg);
        } else {
            if($this->amway){
                $this->element('p', 'instructions',
                _('Use this form to edit your amway infomation.'));
            }else{
                $this->element('p', 'instructions',
                _('Is used amway service,you have to a amway info.'));
            }
        }
    }

    function showScripts() {
        parent::showScripts();
        $this->autofocus('nickname');
    }

    function trySave() {
        $cur = common_current_user();
        $user_id = $cur->id;
        if($this->amway) {
        //is update
            if($this->amway->user_id != $user_id) {
                $this->clientError(_('You must be an user of account to edit the amway info'), 403);
                return;
            }
        }else {
        // is insert
            $this->amway = new Amway_user();
            $this->amway->user_id = $user_id;
            $this->amway->ada = '';
            $this->amway->created_at = common_sql_now();
            $this->amway->is_active = 1;
        }
        $ada    = $this->trimmed('ada');
        $ada_upline    = $this->trimmed('ada_upline');
        $ada_rank    = $this->trimmed('ada_rank');

        if (!Validate::number($ada, array('min' => 1,
            'max' => 9999999)) || strlen($ada) != 7) {
            $this->showForm(_('ADA must have 7 numbers and no spaces.'));
            return;
        }elseif (!Validate::number($ada_upline, array('min' => 1,
            'max' => 9999999)) || strlen($ada_upline) != 7) {
            $this->showForm(_('ADA must have 7 numbers and no spaces.'));
            return;
        }elseif (Amway_user::isExist($ada)&& $ada != $this->amway->ada) {
            $this->showForm(_('Your ADA already in use. Try another one or send to us a photo of ada tag to validate this ADA.'));
            return;
        }
        $this->amway->query('BEGIN');
        $orig = clone($this->amway);
        $this->amway->ada = $ada;
        $this->amway->ada_upline = $ada_upline;
        $this->amway->ada_rank = $ada_rank;
        $this->amway->updated_at = common_sql_now();
        $result = $this->amway->updateKeys($orig);
        $this->amway->query('COMMIT');
        if (!$result) {
            common_log_db_error($this->amway, 'UPDATE', __FILE__);
            $this->serverError(_('Could not update amway info.'));
        }else {
            $this->showForm(_('Amway infomation saved.'));
        }

    }
    function tryInsert() {
        $cur = common_current_user();
        $user_id = $cur->id;
        
        $ada    = $this->trimmed('ada');
        $ada_upline    = $this->trimmed('ada_upline');
        $ada_rank    = $this->trimmed('ada_rank');

        if (!Validate::number($ada, array('min' => 1,
            'max' => 9999999)) || strlen($ada) != 7) {
            $this->showForm(_('ADA must have 7 numbers and no spaces.'));
            return;
        }elseif (!Validate::number($ada_upline, array('min' => 1,
            'max' => 9999999)) || strlen($ada_upline) != 7) {
            $this->showForm(_('ADA must have 7 numbers and no spaces.'));
            return;
        }elseif (Amway_user::isExist($ada)) {
            $this->showForm(_('Your ADA already in use. Try another one or send to us a photo of ada tag to validate this ADA.'));
            return;
        }
        // is insert
        $this->amway = new Amway_user();
        $this->amway->user_id = $user_id;
        $this->amway->created_at = common_sql_now();
        $this->amway->is_active = 1;
        $this->amway->ada = $ada;
        $this->amway->ada_upline = $ada_upline;
        $this->amway->ada_rank = $ada_rank;
        $this->amway->updated_at = common_sql_now();
        $result = $this->amway->insert();
        if (!$result) {
            common_log_db_error($this->amway, 'INSERT', __FILE__);
            $this->serverError(_('Could not insert amway info.'));
        }else {
            $this->showForm(_('Amway infomation saved.'));
        }

    }

}